package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.Planet;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.dialogs.ChooseBuildingDialog;
import ch.unisi.inf.pfii.teamwhite.risk.sound.SoundEngine;

public class SouthPanel extends JPanel{
	
	private Game game;
	
	private GameFrame gameFrame;
	private JPanel imagePanel;
	
	private Planet selectedPlanet;

	public SouthPanel(final Game game, final Planet selectedPlanet, final JPanel imagePanel, final GameFrame gameFrame){
		this.game = game;
		
		this.gameFrame = gameFrame;
		this.imagePanel = imagePanel;
		
		this.selectedPlanet = selectedPlanet;
		
		setLayout(new GridLayout());
		if (!(game.getCurrentPlayer() == null)) {
			add(new PlanetInfoPanel(game, selectedPlanet));
		}
		add(makeGameButtons());
		add(makeSystemButtons());
	}
	
//	This method creates a panel which contains the buttons buy and build
	private Component makeGameButtons(){
		final JPanel gameButtonsPanel = new JPanel();
		gameButtonsPanel.setLayout(new GridLayout());
		
		gameButtonsPanel.add(makeBuy());
		gameButtonsPanel.add(makeBuild());
		
		return gameButtonsPanel;
	}
	
//	This method creates the button buy and creates trough the listener a ship on the selected planet and play a sound
	private Component makeBuy(){
		JButton buy = new JButton("Ship");
		buy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((Human)game.getCurrentPlayer()).buyShip();
				new SoundEngine("buy.wav", gameFrame.soundIsEnabled(), false);
			}
		});
		buy.setEnabled(false);
		if (game.getCurrentPlayer().getType().equals("Human") && ((Human) game.getCurrentPlayer()).getSelectedPlanet() != null && 
				game.getCurrentPlayer().getMoney() >= 1500 && !game.getCurrentPlayer().isDuringAssignment()) {
			buy.setEnabled(true);
		}
		return buy;
	}
	
//	This method creates the button build and creats the dialog where the player can choose the building
	private Component makeBuild(){
		JButton build = new JButton("Build");
		build.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new ChooseBuildingDialog(game, gameFrame);
			}
		});
		build.setEnabled(false);
		if (game.getCurrentPlayer().getType().equals("Human") && ((Human) game.getCurrentPlayer()).getSelectedPlanet() != null && 
				!game.getCurrentPlayer().isDuringAssignment()) {
			build.setEnabled(true);
		}
		return build;
	}

//	This method creates a panel which contains the buttons next and quit
	private Component makeSystemButtons(){
		JPanel systemButtons = new JPanel();
		systemButtons.setLayout(new BorderLayout());
		
		systemButtons.add(gameFrame.makeNext(), BorderLayout.NORTH);
		systemButtons.add(makeQuitButtons(), BorderLayout.SOUTH);
		
		return systemButtons;
	}

	private Component makeQuitButtons(){
		JPanel quitButtons = new JPanel();
		quitButtons.setLayout(new GridLayout());
		
		quitButtons.add(makeQuit());
		
		return quitButtons;
	}
	
//	This method creates the button quit
	private Component makeQuit(){
		JButton quit = new JButton("Quit");
		quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quitIfPossible();
			}
		});
		return quit;
	}
	
//	This method creates a option pane which wait for the confirm
	private void quitIfPossible() {
		ImageIcon icon = new ImageIcon("");
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(SouthPanel.this, "Do you really want to quit?", "Quit?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, icon)) {
            System.exit(0);
        }
    }
}
